<?php

namespace app\controller\admin\cnf;

use app\bll\OrganCompany as BllOrganCompany;
use core\controller\AdminBase;
use Exception;
use support\View;

class OrganCompany extends AdminBase
{
    public function index()
    {
        if (request()->get('type', '') == 'selorgcompany') {
            $BllOrganCompany = new BllOrganCompany();
            $orgs     = $BllOrganCompany->cascader();
            return $this->success(json_decode($orgs, true));
        }

        return $this->view();
    }

    public function add()
    {
        if (is_post()) {
            try {
                $BllOrganCompany = new BllOrganCompany();
                $BllOrganCompany->add(request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view();
        }
    }

    public function edit()
    {
        $id = request()->get('id');
        if (is_post()) {
            try {
                $BllOrganCompany = new BllOrganCompany();
                $BllOrganCompany->edit($id, request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $BllOrganCompany = new BllOrganCompany();
            $info = $BllOrganCompany->find($id);
            View::assign('info', $info);
            return $this->view();
        }
    }

    public function delete()
    {
        $id = request()->get('id');
        try {
            $BllOrganCompany = new BllOrganCompany();
            $BllOrganCompany->delete($id);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
